package org.netty.util;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NettyChannelMap {

    private static Map<String, SocketChannel> channelMap = new ConcurrentHashMap<String, SocketChannel>();


    /**
     * 添加通道
     * @param clientId
     * @param socketChannel
     */
    public static void add(String clientId,SocketChannel socketChannel){
        channelMap.put(clientId,socketChannel);
    }

    /**
     * 获取通道
     * @param clientId
     * @return
     */
    public static Channel get(String clientId){
        return channelMap.get(clientId);
    }

    /**
     * 移除通道
     * @param socketChannel
     */
    public static void remove(SocketChannel socketChannel){
        for (Map.Entry entry:channelMap.entrySet()){
            if (entry.getValue()==socketChannel){
                channelMap.remove(entry.getKey());
            }
        }
    }
}
